﻿using System;
using System.Windows;
using Addins.UI.View;
using WcfClientGenerator.Model.Config;
using WcfClientGenerator.UI.ViewModel;

namespace WcfClientGenerator.UI.View
{
    /// <summary>
    /// The code generation window
    /// </summary>
    public partial class CodeGenerationWindow : WindowBaseView
    {
        #region Instance Fields (Private)

        private WcfClientConfiguration wcfClientConfiguration = null;
        private EnvDTE80.DTE2 applicationObject = null;
        private CodeGenerationViewModel viewModel = null;

        #endregion

        #region Instance Constructors (Public)

        public CodeGenerationWindow(WcfClientConfiguration wcfClientConfiguration, EnvDTE80.DTE2 applicationObject)
        {
            InitializeComponent();

            this.wcfClientConfiguration = wcfClientConfiguration;
            this.applicationObject = applicationObject;

            viewModel = new CodeGenerationViewModel(wcfClientConfiguration, applicationObject);
            this.DataContext = viewModel;

            this.Loaded += new RoutedEventHandler(this.OnLoaded);
            this.Closed += new EventHandler(this.OnClosed);
        }

        #endregion

        #region Event Handlers (Private)

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            ((CodeGenerationViewModel)this.DataContext).StartProcessCommand.Execute(null);
        }

        private void OnClosed(object sender, EventArgs e)
        {
            if (this.viewModel != null)
            {
                this.viewModel.Dispose();
                this.viewModel = null;
            }
        }

        #endregion
    }
}
